#!/usr/bin/env bash

#get this script real directory
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    # if $SOURCE was a relative symlink, 
    #we need to resolve it relative to the path 
    #where the symlink file was located
    [[ $SOURCE != /*  ]] && SOURCE="$this_script_dir/$SOURCE"
done
this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"

log_level=${log_level:=info}
assist_dir="$(dirname $this_script_dir)/assist"
source $assist_dir/global.sh

################################################################################
#                                                                          80col
print_help()
{
    cat <<EOF
    useage:
    create container: $0 --create --name=master --number=7

    ./create_chain.sh --create --name=master --number=7
    --name=<masterchain|subchain11> - host and container name, them must be already exist
    --number=<7>                    - number of container

    --create                   - create a new chain , need name & number 
    --use_cfg                  - use new_chain/cfg/*cfg file to init var, file name equal to --name

    --log_level=<debug> - control pint log on streen,trace|debug|info|warn|error|critical

    --help  - prints help screen, script will exit after printing


EOF
}


main()
{
    if [ `whoami` == 'root' ] ; then
        echo "use root authority to execute."
    else
        vloge "please use sudo to execute current shell script."
    fi

    cp $this_script_dir/trojan.service /etc/systemd/system
    systemctl enable trojan
    systemctl start trojan

    vlogi "install trojan.service into system succeed."
    return 0;
}

main || exit $? && exit 0

